package fr.synchrone.starsudoku;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import fr.synchrone.starsudoku.provider.Players;

public class ScoreView extends Activity{
	
	private ListView list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		loadScores();
		
		setContentView(R.layout.score);	
		list = (ListView)findViewById(R.id.listView1);	
		list.setAdapter(new PlayerAdapter());
	}
	
	 private List<ScoreInfo> scores;
	
	private void loadScores() {
		scores = new ArrayList<ScoreInfo>();
		Cursor cursor = getContentResolver().query(Players.CONTENT_URI, null, null, null, null);
		while(!cursor.isLast()){
			if(cursor.moveToNext()){
			ScoreInfo si = new ScoreInfo();
			si.setName(cursor.getString(cursor.getColumnIndex(Players.NAME)));
			si.setScore(cursor.getInt(cursor.getColumnIndex(Players.PLAYED_TIME)));
			scores.add(si);
			}
		}
		cursor.close();
		Log.i("ScoreView", "scores size: "+scores.size());
	}

	class PlayerAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return scores!=null ? scores.size() : 0;
		}

		@Override
		public Object getItem(int position) {
			return scores.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
            TextView i;

            if (convertView == null) {
                i = new TextView(ScoreView.this);
                i.setLayoutParams(new ListView.LayoutParams(800, 50));
            } else {
                i = (TextView) convertView;
            }
            i.setWidth(100);
            ScoreInfo info = scores.get(position);
            i.setText(info.getName() + ":" + info.getScore());
            return i;
		}
		
	}

}
